/*******************************************************************************
 * 文件名: CsvImport.java
 * 描    述: Csv导入类
 * 创建人: Dean Huang(hudi-112@163.com)
 * 日　期: 2019-04-23
 * 描　述: 文件描述
 * 版　本: 1.0
 *******************************************************************************/

package com.z1.core.data.imports.csv;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.List;

import com.z1.core.data.DataConfig;
import com.z1.core.data.DataConfigCell;
import com.z1.core.data.imports.DataImport;
import com.z1.core.exception.Z1Exception;
import com.z1.core.reflect.BeanReflect;
import com.z1.core.reflect.ObjectReflect;

public class CsvImport extends DataImport {

	//文件读取流对象
	private BufferedReader reader = null;
	//字段分隔符
	private String delimiter = ",";
	
	//进度
	//总大小
	private long sumSize = 0L;
	//当前大小
	private long curSize = 0L;
	
	public void setDelimiter(String delimiter) {
		this.delimiter = delimiter;
	}
	
	protected CsvImport(DataConfig dataConfig, File file, boolean containHeader) {
		super(dataConfig, file, containHeader);
		try {
			if (file.getName().endsWith(".csv")) {
				//IO
				reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
				sumSize = file.getTotalSpace();
			}
			else {
				throw new IllegalArgumentException("仅支持csv文件格式");
			}
			
			if (containHeader) {
				reader.readLine();
			}
		}
		catch (Exception e) {
			close();
			throw new Z1Exception(e);
		}
	}
	
	@Override
	public boolean nextData() {
		if (null == reader) {
			return false;
		}
		try {
			valueList.clear();
			String line = null;
			while (null != (line = reader.readLine())) {
				getRow(line.split(delimiter));
				curSize += line.getBytes().length;
				cur++;
				if (0 != cur && 0 == cur % step) {
					break;
				}
			}
		}
		catch (Exception e) {
			throw new Z1Exception(e);
		}
		return !valueList.isEmpty();
	}
	
	/**
	 * 获取行数据
	 * @param row
	 */
	private void getRow(String[] row) throws Exception {
		Object value = dataConfig.getBeanClass().newInstance();
		try {
			//固定字段
			List<DataConfigCell> cells = dataConfig.getImportCells();
			for (int i = 0; i < cells.size(); i++) {
				DataConfigCell dataConfigCell = cells.get(i);
				if (null == dataConfigCell) {
					continue;
				}
				if (row.length <= i) {
					continue;
				}
				Object obj = ObjectReflect.getObject(
					dataConfigCell.getJavaType(), 
					dataConfigCell.getLength(), 
					dataConfigCell.getPrecision(), 
					row[i]
				);
				if (null == obj && dataConfigCell.getRequired()) {
					//TODO: 必填参数
				}
				BeanReflect.setObjectToT(value, dataConfigCell.getProperty(), obj, dataConfigCell.getJavaTypeClass());
			}
			valueList.add(value);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 进度(保留2为小数)
	 * @return
	 */
	@Override
	public double getSeek() {
		int seek = (int)(curSize / (double)sumSize * 100);
		return seek/(double)100;
	}
	
	@Override
	public void close() {
		try {
			if (null != reader) {
				reader.close();
				reader = null;
			}
		}
		catch (Exception ex) {}
	}

	public static CsvImport parse(DataConfig dataConfig, File f) {
		return new CsvImport(dataConfig, f, true);
	}
	public static CsvImport parse(DataConfig dataConfig, File f, boolean skipHeader) {
		return new CsvImport(dataConfig, f, skipHeader);
	}
	
}
